package com.example.testproject.ext

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.testproject.R
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.RoundedCornerTreatment
import com.google.android.material.shape.ShapeAppearanceModel
import com.jakewharton.rxbinding2.view.RxView
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit
import kotlin.math.roundToInt


/**
 *
 * @ClassName: ViewExt
 * @Author: android 超级兵
 * @CreateDate: 10/11/21$ 5:40 PM$
 * TODO View 的一些扩展
 */

fun <T> ImageView.loadUrl(url: T, width: Int = 900, height: Int = 1600, angle: Int = 1) {
    //设置图片圆角
    val roundedCorners = RoundedCorners(angle)
    //通过RequestOptions扩展功能
    val options: RequestOptions =
        RequestOptions.bitmapTransform(roundedCorners)
            .placeholder(R.mipmap.no_data) //设置占位图
            .error(R.mipmap.no_data) //设置错误图片
            .fallback(R.mipmap.no_data)
            .override(width, height) //采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗

    Glide.with(context)
        .load(url)
        .apply(options) //fixme 注意加载圆角的时候，注释掉下面的方法
        .into(this)
}

/*
 * 作者:android 超级兵
 * 创建时间: 9/15/21 2:42 PM
 * TODO  文字加粗
 */
fun TextView.bold() {
    typeface = Typeface.defaultFromStyle(Typeface.BOLD)
}


//TODO 利用RxView机制 防止View重复点击
fun View.click(
    // 时间[单位:秒(s)]
    time: Int = 1,
    click: (View) -> Unit,
) {
    RxView.clicks(this).throttleFirst(time.toLong(), TimeUnit.SECONDS)
        .subscribe(object : io.reactivex.Observer<Any> {
            override fun onSubscribe(d: Disposable) {
            }

            override fun onNext(value: Any) {
                click(this@click)
            }

            override fun onError(e: Throwable) {
            }

            override fun onComplete() {
            }
        })
}

/*
 * 作者:android 超级兵
 * 创建时间: 10/28/21 9:44 AM
 * TODO  EditText输入扩展
 */
fun EditText.addOnTextChangedClick(onTextChanged: (String) -> Unit) {
    this.addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            onTextChanged(s.toString())
        }

        override fun afterTextChanged(s: Editable?) {
        }
    })
}



/*
 * 作者:android 超级兵
 * 创建时间: 12/24/21 10:48 AM
 * TODO 设置圆角
 */
@SuppressLint("RestrictedApi")
fun View.shadowBackground(
    context: Context,
    angle: Float = 16f, // 默认圆角大小
    backColor: String = "#05bebebe", // 自身颜色
    shadowColor: String = "#D2D2D2", // 阴影颜色
    shadowOffset: Int = 2, // 阴影 Offset 偏移量

) = let {
    val shapePathModel = ShapeAppearanceModel.builder()
        .setAllCorners(RoundedCornerTreatment())
        .setAllCornerSizes(angle)
        .build()

    val backgroundDrawable = MaterialShapeDrawable(shapePathModel).apply {
        // 设置本身颜色
        setTint(Color.parseColor(backColor))
        paintStyle = Paint.Style.FILL
        shadowCompatibilityMode = MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS
//            shadowCompatibilityMode = MaterialShapeDrawable.SHADOW_COMPAT_MODE_DEFAULT
        initializeElevationOverlay(context)
        // 圆角 四舍五入
        shadowRadius = angle.roundToInt()
        // 阴影颜色
        setShadowColor(Color.parseColor(shadowColor))
//            setShadowColor(Color.parseColor("#FF00FF"))
        // 阴影深度【偏移量】
        shadowVerticalOffset = shadowOffset
    }
    (this.parent as? ViewGroup)?.clipChildren = false
    this.background = backgroundDrawable
}